package com.cuit.qx.controller;


import com.cuit.qx.aop.OperLog;
import com.cuit.qx.bean.RoleGroup;
import com.cuit.qx.bean.RoleNode;
import com.cuit.qx.bean.User;
import com.cuit.qx.service.RoleManagerService;
import com.cuit.qx.service.RoleNodeService;
import com.cuit.qx.utils.exception.ServiceException;
import com.cuit.qx.utils.tools.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 * @author Administrator
 *
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping(value="/api/RoleManagerAction")
public class RoleManagerAction {

	@Autowired
	private RoleManagerService roleManagerService;
	@Autowired
	private RoleNodeService roleNodeService;


	/**
	 * 查询列表
	 * @return
	 */
	@RequestMapping(value="/getRoleListInfo")
	@OperLog(operModul = "角色管理", operType = "1", operDesc = "根据条件查询角色列表")
	@ResponseBody
	public Map<String, Object> getRoleListInfo(String searchKey, String roleName, String roleStatus, Integer page, Integer rows){
		List<RoleGroup> pageList = roleManagerService.queryRolePageList(searchKey,roleName,roleStatus,page,rows);
		int count = roleManagerService.queryRolePageListCount(searchKey, roleName, roleStatus);
		Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 20000);
        result.put("data", pageList);
        result.put("total",count);
		return result;
	}

	/**
	 * 查询用户列表的角色
	 * @param
	 * @return
	 */
	@RequestMapping(value="/getRoleListByUser")
	@OperLog(operModul = "角色管理", operType = "1", operDesc = "查询用户列表的角色")
	@ResponseBody
	public List<RoleGroup> getRoleListByUser(HttpServletRequest request){
		List<RoleGroup> list = roleManagerService.getRoleListByUser();
		return list;
	}


	/**
	 * 删除角色
	 * @return
	 */
	@RequestMapping(value="/deleteRole")
	@OperLog(operModul = "角色管理", operType = "4", operDesc = "删除角色")
	@ResponseBody
	public Map<String, Object> deleteRole(String id){
		if(StringUtils.isEmpty(id)){
			throw new ServiceException("传入参数异常");
		}
        Map<String, Object> result1 = new HashMap<String, Object>();
		Map<String,Object> result = new HashMap<String,Object>();
		if(roleManagerService.deleteRole(id)>0){
			result.put("success", true);
	        result1.put("code", 20000);
	        result1.put("data", result);
			return result1;
		}
		result.put("success", false);

        result1.put("code", 20000);
        result1.put("data", result);
		return result1;
	}


	/**
	 * 添加角色
	 * @return
	 */
	@RequestMapping(value="/addRole")
	@OperLog(operModul = "角色管理", operType = "2", operDesc = "添加角色")
	@ResponseBody
	public Map<String, Object> addRole(HttpServletRequest request, @RequestBody RoleGroup role){
		role.setGroupId(StringUtils.getPrimarykeyId());
//		User user =  (User)request.getSession().getAttribute("user");
//		role.setAddUser(user.getUserId());
		role.setAddDate(new Date());
        Map<String, Object> result = new HashMap<String, Object>();		
		if(roleManagerService.addRole(role)>0){
	        result.put("code", 20000);
	        result.put("data", "true");
	        return result;
		}
        result.put("code", 20001);
        result.put("data", "false");
        return result;
	}

	/**
	 * 验证角色是否存在
	 * @return
	 */
	@RequestMapping(value="/checkRoleName")
	@ResponseBody
	public Map<String, Object> checkRoleName(String roleName){
		int count = roleManagerService.checkRoleName(roleName);
		Map<String,Object> result = new HashMap<String,Object>();
		if(count>0){
			result.put("success", false);
		}else {
			result.put("success", true);	
		}
        Map<String, Object> result1 = new HashMap<String, Object>();
        result1.put("code", 20000);
        result1.put("data", result);
		return result1;

	}

	/**
	 * 查询角色信息
	 * @return
	 */
	@RequestMapping(value="/getRoleInfoById")
	@OperLog(operModul = "角色管理", operType = "1", operDesc = "根据角色id查询角色信息")
	@ResponseBody
	public RoleGroup getRoleInfoById(String roleId){
		return roleManagerService.queryRoleBeanInfo(roleId);
	}

	/**
	 * 编辑角色
	 * @return
	 */
	@RequestMapping(value="/updateRoleInfo")
	@OperLog(operModul = "角色管理", operType = "3", operDesc = "编辑角色")
	@ResponseBody
	public Map<String, Object> updateRoleInfo(@RequestBody RoleGroup role){
		int flag = roleManagerService.updateRole(role);
		Map<String,Object> result = new HashMap<String,Object>();
		if(flag>0){
			result.put("success", true);
		}else {		
			result.put("success", false);
		}
        Map<String, Object> result1 = new HashMap<String, Object>();
        result1.put("code", 20000);
        result1.put("data", result);
		return result1;
	}
	
	
	/**
	 * 更新角色资源
	 * @param
	 * @return
	 */
	@RequestMapping(value="/updateRoleSourceInfo")
	@OperLog(operModul = "角色管理", operType = "3", operDesc = "更新角色资源(菜单)")
	@ResponseBody
	public Map<String, Object> updateRoleSourceInfo(String authIds,String id,String bnode){
		String[] source = authIds.split(",");
		boolean flag = roleManagerService.isExsitsRoleMenu(id);
		RoleNode roleid = roleNodeService.getById(id);
		if(flag){
			//资源存在则修改role_menu_status为1
			roleManagerService.updateRoleMenuStatusByRoleId(id);
			//然后根据提交的menu修改状态0
			roleManagerService.updateRoleMenuPermission(id,source);
		}else{
			//不存在，则第一次添加
			roleManagerService.addRoleMenuList(source,id);
		}
		RoleNode roleNode=new RoleNode();
		roleNode.setRoleId(id);
		roleNode.setHalfNode(bnode);
		if(roleid==null){
			roleNodeService.save(roleNode);
		}else{
			roleNodeService.updateById(roleNode);
		}
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("success", true);
        Map<String, Object> result1 = new HashMap<String, Object>();
        result1.put("code", 20000);
        result1.put("data", result);
		return result1;

	}
	
	/**
	 * 改变角色的状态
	 * @param roleId
	 * @param status
	 * @return
	 */
	@RequestMapping(value="/changeRoleStatus")
	@OperLog(operModul = "角色管理", operType = "3", operDesc = "改变角色的状态")
	@ResponseBody
	public Map<String, Object> changeRoleStatus(String roleId,String status){
		int falg = roleManagerService.changeRoleStatus(roleId,status);
		Map<String,Object> result = new HashMap<String,Object>();
		if(falg > 0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
        Map<String, Object> result1 = new HashMap<String, Object>();
        result1.put("code", 20000);
        result1.put("data", result);
		return result1;

	}
}
